{% extends './admin/base/base.html.twig' %}

{% block content %}
    <div class="page-title">
        <h3 class="breadcrumb-header">
            编辑用户组
            {% if hasPermission('USER_GROUP_INDEX') %}
            <a href="{{ path(constant_route('USER_GROUP_INDEX')) }}" class="btn btn-primary"><i class="fa fa-undo"></i>返回</a>
            {% endif %}
        </h3>
    </div>
    <div id="main-wrapper">
        <div class="row">
            <div class="col-md-6">
                <div class="panel panel-white">
                    <div class="panel-heading">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label for="title" class="col-sm-2 control-label">用户组名称：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" value="{{ info.info.groupName }}" id="group-name">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="title" class="col-sm-2 control-label">规则名称：</label>
                                <div class="col-sm-10">
                                    <select id="role" class="form-control" multiple style="height:300px">
                                        <option value="">请选择...</option>
                                        {% for role in roleList %}
                                            <option value="{{ role.id }}" {% if role.id in info.userGroupRole %}selected{% endif %} >{{ role.roleName }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="input-Default" class="col-sm-2 control-label"></label>
                                <div class="col-sm-10">
                                    <button type="reset" id="clear" class="btn btn-default"><i class="fa fa-undo"></i> 重置</button>
                                    {% if hasPermission('USER_GROUP_UPDATE') %}
                                    <button type="button" class="btn btn-primary" id="save"><i class="fa fa-save"></i> 保存</button>
                                    {% endif %}
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="panel-body" id="permission-tree" data-load-url="">
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
    <script type="text/javascript">
        $(function () {
            $('#clear').click(function(){
                $('#permission-tree').reload();
            });
            loadPermission();
            $('#role').change(loadPermission);
            $('#save').click(function () {
                let id = '{{ info.info.id }}'
                let roleId = $('#role').val();
                let groupName = $('#group-name').val().trim();
                if(groupName === ""){
                    layer.msg("请输入用户组名",{timeout:1500},function(){
                        $('#group-name').focus();
                    });
                    return false;
                }
                if(roleId.length === 0){
                    layer.msg("请选择规则",{timeout:1500},function(){
                        $('#role').focus();
                    });
                    return false;
                }
                Tools.ajax({
                    url:"{{ path(constant_route('USER_GROUP_UPDATE')) }}",
                    data:{id, groupName, roleId},
                    success:function(res){
                        layer.msg(res.msg,{time:1500},function(){
                            window.location.href = '{{ path(constant_route('USER_GROUP_INDEX')) }}';
                        });
                    }
                });
            });
        });
    </script>
{% endblock %}